package com.zslibrary.Protocol;

import android.os.Build;

import com.zslibrary.Enum.BoardId;
import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.StateHandle.BusyState;
import com.zslibrary.StateHandle.ErrorState;
import com.zslibrary.StateHandle.GateQueryState;
import com.zslibrary.StateHandle.IdleState;
import com.zslibrary.StateHandle.InitState;
import com.zslibrary.StateHandle.ReconnectState;
import com.zslibrary.StateHandle.StateMachine;
import com.zslibrary.Structure.ConfigRecipeStrcut;
import com.zslibrary.Structure.HardwareVersionStruct;
import com.zslibrary.Structure.SoftwareVersionStruct;
import com.zslibrary.Structure.TestInfosStruct;
import com.zslibrary.Structure.UserAdjustmentStruct;

/*将外部使用指令集合到一起，整合pipe,state machine*/
public class UserCommand {
    private Pipe mPipe;
    private StateMachine mStateMachine;

    public UserCommand(Pipe pipe,StateMachine stateMachine){
        mPipe=pipe;
        /*注册状态*/
        mStateMachine=stateMachine;
        mStateMachine.startStateMachine();  /*启动状态机*/
    }

    public @StateDefine.State int getCurrentState(){
        return mStateMachine.getCurrentState();
    }

    public void setAllUserAdjustment(final UserAdjustmentStruct[] adjustments){
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            int returnEvent=Adjustment.setAllUserAdjustment(mPipe,adjustments);
            mStateMachine.resumeStateMachine(returnEvent);
            return;
        }
        mStateMachine.resumeStateMachine(event);    //未执行，返回获取的事件
    }

    public TestInfosStruct[] getTestInfos(final int componentId){
        int event=mStateMachine.pauseStateMachine();
        TestInfosStruct[] infos=null;
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            ComponentTest test=new ComponentTest();
            int returnEvent=test.getTestInfos(mPipe,(short)componentId);
            infos=test.getTestInfos();
            mStateMachine.resumeStateMachine(returnEvent);  //执行成功，返回执行之后的事件
            return infos;
        }
        mStateMachine.resumeStateMachine(event);    //未执行，返回获取的事件
        return infos;
    }

    public void componentTest(int componentId,int funcId){
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            ComponentTest test=new ComponentTest();
            int returnEvent=test.componentTest(mPipe,(short)componentId,(short)funcId);
            mStateMachine.resumeStateMachine(returnEvent);
            return;
        }
        mStateMachine.resumeStateMachine(event);
    }

    public UserAdjustmentStruct setCalibrationValue(int componentId,int tryTimes,int value){
        int event=mStateMachine.pauseStateMachine();
        UserAdjustmentStruct adjustment=null;
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            ComponentTest test=new ComponentTest();
            int returnEvent=test.putCalibrationValue(mPipe,(short)componentId,tryTimes,value);
            adjustment=test.putCalibrationValue();
            mStateMachine.resumeStateMachine(returnEvent);
            return adjustment;
        }
        mStateMachine.resumeStateMachine(event);
        return adjustment;
    }

    public void startMackeProduct(ConfigRecipeStrcut[] recipe,int craftId){
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            int returnEvent=MakeProduct.makeProduct(recipe,craftId,mPipe);
            mStateMachine.resumeStateMachine(returnEvent);
            return;
        }
        mStateMachine.resumeStateMachine(event);
    }


    Version mVersion=new Version();
    public HardwareVersionStruct[] getHardwareVersions(){
        HardwareVersionStruct[] versions=null;
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            int returnEvent=mVersion.getBoardHardwareVersion(mPipe);
            mStateMachine.resumeStateMachine(returnEvent);
            versions=mVersion.getBoardHardwareVersion();
        }else
            mStateMachine.resumeStateMachine(event);
        if(versions==null){
            versions=new HardwareVersionStruct[1];
            versions[0].boardAdress=0;
            versions[0].version="unknown version";
        }
        return versions;
    }

    public SoftwareVersionStruct[] getSoftwareVersions(){
        SoftwareVersionStruct[] versions=null;
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            int returnEvent= mVersion.getBoardSoftwareVersions(mPipe);
            mStateMachine.resumeStateMachine(returnEvent);
            versions=mVersion.getBoardSoftwareVersions();
        } else
            mStateMachine.resumeStateMachine(event);
        if(versions==null)
            versions=new SoftwareVersionStruct[0];
        return versions;
    }

    public int updateSoftware(@BoardId.BoardAdress int boardAddress, int type, String filePath){
        int event=mStateMachine.pauseStateMachine();
        if(event==StateEventDefine.EVT_NONE||event==StateEventDefine.EVT_QUERY_GATE){
            int returnEvent=mVersion.updateSoftWare(mPipe,boardAddress,type,filePath);
            mStateMachine.resumeStateMachine(returnEvent);
            return mVersion.getUpdateResult();
        }
        mStateMachine.resumeStateMachine(event);
        return Version.UPDATE_FAILED;
    }

    public int getUpdateProgress(){
        return mVersion.getUpdateProgress();
    }
}
